<template>
  <div class="login-container">
    <!-- <div class="maskLayer" /> -->
    <el-form
      ref="loginRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <!-- 头像 -->
      <div class="title-container">
        <img src="@/assets/qqq.webp" alt="">
      </div>
      <el-form-item prop="loginName" class="allborder">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.loginName"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password" class="allborder">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          ref="password"
          v-model="loginForm.password"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          :type="passwordType"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <el-form-item class="formItem allborder" prop="vertification">
        <el-input
          v-model="loginForm.code"
          placeholder="请输入验证码"
          @keyup.enter.native="handleLogin"
        />
        <div class="login-img">
          <img :src="`http://likede2-admin.itheima.net/likede/api/user-service/user/imageCode/${loginForm.clientToken}`">
        </div>
      </el-form-item>

      <el-button
        class="loginBtn"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
      >Login</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        loginType: 0, // 登陆类型
        code: '', // 验证码
        clientToken: Math.random().toString(36).substring(2) // 随机数token
      },
      loginRules: {
        loginName: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 4, max: 4, message: '请输入正确验证码', trigger: 'blur' }
        ]
      },
      passwordType: 'password',
      redirect: undefined
    }
  },

  methods: {
    // 改变密码小眼睛状态
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = 'text'
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 点击登录执行的方法
    handleLogin() {
      this.$refs.loginRef.validate(async valid => {
        if (!valid) return
        try {
          const res = await this.$store.dispatch('user/participateLogin', this.loginForm)
          if (res.success) {
            this.$message.success(res.msg)
            this.$router.push('/')
          }
        } catch (err) {
          console.log(err)
        }
      })
    }
  }
}
</script>

<style lang="scss">
// 全局生效的样式
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #000;
$cursor: #000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
    border-radius: 5px;
    color: #454545;
  }

  .el-form-item__error {
    color: #fff;
  }
}
</style>

<style lang="scss" scoped>
// 只对当前组件生效的样式
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.el-form{
  margin-top: 30% !important;
  padding: 0 !important;
  transform: translateY(-50%);
  background-color: #fff;
  border-radius: 20px;
  border: 8px solid #9973a8;
  position: fixed;
  z-index: 999;
}
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url("~@/assets/keqing.webp"); // 设置背景图片
  background-position: center; // 将图片位置设置为充满整个屏幕
  background-size: cover;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .loginBtn {
    background: #9973a8;
    width: 90% !important;
    height: 64px;
    line-height: 32px;
    font-size: 24px;
   margin-left: 25px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .title-container{
    width: 80px;
    height: 80px;
    overflow: hidden;
    border-radius: 50%;
    border: 5px solid #9973a8;
    margin-left: 50%;
    margin-top:15px ;
    transform: translateX(-50%);
    img{
      width: 100%;
      height: 100%;
    }
  }

}
.login-img{
  position: absolute;
  right: 0;
  top: 0;
}
.allborder{
  border: 1px solid #000;
  width: 90% !important;
  margin:20px auto ;
}
</style>
